package ui.photo
{
	
	import common.util.SystemUtil;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.events.MouseEvent;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.utils.ByteArray;
	
	import mx.controls.Button;
	import mx.controls.Label;
	import mx.controls.Spacer;
	import mx.graphics.ImageSnapshot;
	import mx.graphics.codec.JPEGEncoder;
	import mx.managers.HistoryManager;
	import mx.managers.IHistoryManagerClient;
	import mx.managers.PopUpManager;
	import mx.utils.ObjectUtil;
	
	import spark.components.BorderContainer;
	import spark.components.HGroup;
	import spark.components.VGroup;
	import spark.layouts.VerticalLayout;
	
	public class PopImageContainer extends BorderContainer
	{
		//图片初始旋转角度，0
		private var intialValue:int=0;
		[Embed(source="resources/image/photo/leftRotation.png")]
		public static var leftRotationClass:Class;
		[Embed(source="resources/image/photo/rightRotation.png")]
		public static var rightRotationClass:Class;
		[Embed(source="resources/image/photo/amp.png")]
		public static var ampClass:Class;
		[Embed(source="resources/image/photo/nar.png")]
		public static var narClass:Class;
		[Embed(source="resources/image/photo/restore.png")]
		public static var restoreClass:Class;
		[Embed(source="resources/image/photo/save.png")]
		public static var saveClass:Class;
		[Embed(source="resources/image/photo/del.png")]
		public static var delClass:Class;
		
		public function PopImageContainer(img:Object,labelCtn:String)
		{
			super();
			
/*			var vLayout:VerticalLayout=new VerticalLayout();
			vLayout.verticalAlign="middle";
			vLayout.horizontalAlign="center";
			this.layout=vLayout;
			vLayout.gap=0;*/
			
			var topGroup:VGroup=new VGroup();
			topGroup.gap=0;
			topGroup.verticalAlign="middle";
			topGroup.horizontalAlign="center";
			topGroup.percentWidth=100;
			topGroup.percentHeight=100;
			
			var allCtnImgGroup:BorderContainer=new BorderContainer();
			var ctnImgLayout:VerticalLayout=new VerticalLayout();
			ctnImgLayout.verticalAlign="middle";
			ctnImgLayout.horizontalAlign="left";
			ctnImgLayout.gap=0;
			allCtnImgGroup.layout=ctnImgLayout;
			
			var ctnImgGroup:HGroup=new HGroup();
			ctnImgGroup.verticalAlign="top";
			ctnImgGroup.gap=-16;
			
			var optBtnGroup:HGroup=new HGroup();
			optBtnGroup.verticalAlign="bottom";
			optBtnGroup.horizontalAlign="center";
			optBtnGroup.percentWidth=100;
			optBtnGroup.percentHeight=100;
			optBtnGroup.paddingBottom=60;
			var space:Spacer=new Spacer();
			space.percentHeight=100;
			optBtnGroup.addElement(space);
			
			var imgGroup:BorderContainer=new BorderContainer();
			imgGroup.setStyle("borderStyle","solid");
			imgGroup.setStyle("borderColor","0xCCCCCC");
			imgGroup.setStyle("borderVisible","true");
			imgGroup.setStyle("borderWeight","5");
			var imgTarget:Object=null;
			
			if(img as mx.controls.Image){
				imgTarget=new mx.controls.Image();
				var data:BitmapData = Bitmap((img as mx.controls.Image).content).bitmapData;		
				imgTarget.width = data.width;
				imgTarget.height = data.height;
				
				imgTarget.source = new Bitmap(data);
				imgGroup.addElement(imgTarget as mx.controls.Image);
				
				
			}else{
				imgTarget=new spark.components.Image ();
				imgTarget.source=img.source;
				imgTarget.height=img.bitmapData.height;
				imgTarget.width=img.bitmapData.width;
				
				imgGroup.addElement(imgTarget as spark.components.Image);
			}

			
			var leftRotationBtn:Button=new Button();
			leftRotationBtn.label="左旋转";
			leftRotationBtn.setStyle("icon",leftRotationClass);
			leftRotationBtn.addEventListener(MouseEvent.CLICK,function(){
				var m:Matrix = imgGroup.transform.matrix;
				var p:Point = m.transformPoint(new Point(imgGroup.width/2, imgGroup.height/2));
				m.translate(-p.x, -p.y);
				m.rotate(-Math.PI / 180 * 90 );
				m.translate(p.x, p.y);
				imgGroup.transform.matrix = m;
				intialValue=intialValue-90;
			});
			optBtnGroup.addElement(leftRotationBtn); 
			
			var rightRotationBtn:Button=new Button();
			rightRotationBtn.label="右旋转";
			rightRotationBtn.setStyle("icon",rightRotationClass);
			rightRotationBtn.addEventListener(MouseEvent.CLICK,function(){
				var m:Matrix = imgGroup.transform.matrix;
				var p:Point = m.transformPoint(new Point(imgGroup.width/2, imgGroup.height/2));
				m.translate(-p.x, -p.y);
				m.rotate(Math.PI / 180 * 90 );
				m.translate(p.x, p.y);
				imgGroup.transform.matrix = m;	
				intialValue=intialValue+90;
			});
			optBtnGroup.addElement(rightRotationBtn);
			
			var scaleMaxBtn:Button=new Button();
			scaleMaxBtn.label="放大";
			scaleMaxBtn.setStyle("icon",ampClass);
			scaleMaxBtn.addEventListener(MouseEvent.CLICK,function(){
				if(imgTarget.scaleX<1.5){
					imgTarget.scaleX = imgTarget.scaleX+0.1 ;
					imgTarget.scaleY = imgTarget.scaleY+0.1;
					imgGroup.width=imgTarget.scaleX*imgTarget.width+10;
					imgGroup.height=imgTarget.scaleY*imgTarget.height+10;
				}
				
			});
			optBtnGroup.addElement(scaleMaxBtn);
			
			var scaleMinBtn:Button=new Button();
			scaleMinBtn.label="缩小";
			scaleMinBtn.setStyle("icon",narClass);
			scaleMinBtn.addEventListener(MouseEvent.CLICK,function(){
				if(imgTarget.scaleX>0.5){
					imgTarget.scaleX = imgTarget.scaleX-0.1 ;
					imgTarget.scaleY = imgTarget.scaleY-0.1;
					imgGroup.width=imgTarget.scaleX*imgTarget.width+10;
					imgGroup.height=imgTarget.scaleY*imgTarget.height+10;
				}
				
			});
			optBtnGroup.addElement(scaleMinBtn);
			
			var reduceBtn:Button=new Button();
			reduceBtn.label="还原";
			reduceBtn.setStyle("icon",restoreClass);
			reduceBtn.addEventListener(MouseEvent.CLICK,function(){
				imgTarget.scaleX=1;
				imgTarget.scaleY=1;
				imgGroup.width=imgTarget.width+10;
				imgGroup.height=imgTarget.height+10;
				
				var m:Matrix = imgGroup.transform.matrix;
				var p:Point = m.transformPoint(new Point(imgGroup.width/2, imgGroup.height/2));
				m.translate(-p.x, -p.y);
				m.rotate(-Math.PI / 180 * intialValue );
				m.translate(p.x, p.y);
				imgGroup.transform.matrix = m;	
				intialValue=0;
			});
			optBtnGroup.addElement(reduceBtn);
			
			var saveBtn:Button=new Button();
			saveBtn.label="另存为";
			saveBtn.setStyle("icon",saveClass);
			saveBtn.addEventListener(MouseEvent.CLICK,function():void{
				var en:JPEGEncoder = new JPEGEncoder(100); //压缩图片，100是指质量
				var ba:ByteArray=null;
				if(img as mx.controls.Image){
					ba=en.encode(ImageSnapshot.captureBitmapData(imgTarget as mx.controls.Image));//将控件转为BitmapData后再转为ByteArray
				}else{
					ba=en.encode(ImageSnapshot.captureBitmapData(imgTarget as spark.components.Image));//将控件转为BitmapData后再转为ByteArray
				}
				var request:URLRequest = new URLRequest(SystemUtil.getWebUrl()+"downloadPhotoServlet");
				request.method="POST";
				request.data=ba;
				request.contentType = "application/octet-stream";  //这个很重要，设置成流数据
				navigateToURL(request,"_blank");    //因为要浏览器触发下载事件，所以就不用异步方式打开连接了
			});
			optBtnGroup.addElement(saveBtn);
			
			var closeBtn:Button=new Button();
			closeBtn.label="关闭";
			closeBtn.setStyle("icon",delClass);
			closeBtn.addEventListener(MouseEvent.CLICK,closePopWindow);
			optBtnGroup.addElement(closeBtn);
			
			var labelGroup:HGroup=new HGroup();
			labelGroup.paddingLeft=16;
			labelGroup.verticalAlign = "middle";
			labelGroup.horizontalAlign = "left";
			labelGroup.percentWidth = 100;
			var ctnLabel:Label=new Label();
			ctnLabel.text=labelCtn;//"车牌号：浙A66666\n拍照时间：2011-10-10 09：02：02\n速度：90km/h\n地理位置：浙江省杭州市西湖区天目山路160号国际花园东楼14楼中导科技开发有限公司";
			ctnLabel.percentWidth=100;
			ctnLabel.height=70;
			ctnLabel.setStyle("color","green");
			labelGroup.addElement(ctnLabel);
			
			ctnImgGroup.addElement(imgGroup);

			
			allCtnImgGroup.addElement(ctnImgGroup);
			allCtnImgGroup.addElement(labelGroup);
			topGroup.addElement(allCtnImgGroup);

			this.setStyle("borderVisible","false");
			this.setStyle("backgroundAlpha","0.5");
			this.setStyle("backgroundColor","0x000000");
			this.addElement(topGroup);
			this.addElement(optBtnGroup);

		}
		
		public function closePopWindow(event:MouseEvent):void{
			PopUpManager.removePopUp(this);
		}

		

	}
}